﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bilibili.Model.Live;
using Bilibili.Model.Resource;
using Bilibili.Common.Log;
using Bilibili.Common;
using PCLUntils.IEnumerables;
using PCLUntils.Objects;

namespace Bilibili.WebClient.Api
{
    public sealed class LiveHomeClient : ClientBase
    {
        public event CallbackBooleanObject OnLoadEvent;
        public LiveBanner Banner { get; set; }
        public LiveAreaEntrance Areas { get; set; }
        public LiveHourRank HourRank { get; set; }
        public List<RoomList> RoomList { get; set; }
        public async Task LoadHome()
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("https://api.live.bilibili.com/xlive/app-interface/v2/index/getAllList", "&actionKey=appkey&qn=0&rec_page=1&relation_page=1&scale=xxhdpi", false);
                var results = await ApiClient.GetResults(apiInfo);
                var model = results.ToDynamicJObject();
                if (model.IsNotEmpty())
                {
                    if (model.Code == 0)
                    {
                        LiveHome m = model.Json["data"].ToObject<LiveHome>();
                        Areas = m.area_entrance_v2.GetItem<LiveAreaEntrance>(0);
                        Banner = m.banner.GetItem<LiveBanner>(0);
                        HourRank = m.hour_rank.GetItem<LiveHourRank>(0);
                        RoomList = m.room_list;
                    }
                    OnLoadEvent?.Invoke(OnLoadEvent, model.Code == 0);
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("初始化直播间数据", ex);
            }
        }
        public async Task<ReturnModel<List<AreaList>>> GetAreaList()
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("https://api.live.bilibili.com/room/v1/Area/getList", "&actionKey=appkey&need_entrance=1&parent_id=0", false);
                var results = await ApiClient.GetResults(apiInfo);
                var model = results.ToDynamicJObject();
                if (model.IsNotEmpty())
                {
                    if (model.Code == 0)
                    {
                        var m = model.Json["data"].ToObject<List<AreaList>>();
                        var rec = await GetRecAreaList();
                        if (rec.Success)
                        {
                            m.Insert(0, new AreaList
                            {
                                id = 0,
                                name = "推荐",
                                list = rec.Data
                            });
                        }
                        return BuildSuccessResult(m);
                    }
                    else
                        return BuildFailedResult<List<AreaList>>(model.Message);
                }
                else
                    return BuildFailedResult<List<AreaList>>();
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("GetAreaList", ex);
                return BuildExceptionResult<List<AreaList>>(ex);
            }
        }
        public async Task<ReturnModel<ReturnJObject>> GetHot(int hotIndex)
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("https://api.live.bilibili.com/room/v3/Area/getRoomList", $"&actionKey=appkey&area_id=0&cate_id=0&page={hotIndex}&page_size=30&parent_area_id=0&qn=0&sort_type=online&tag_version=1", true);
                string results = await ApiClient.GetResults(apiInfo);
                var m = results.ToDynamicJObject();
                return BuildSuccessResult(m);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<ReturnJObject>(ex);
            }
        }
        public async Task<ReturnModel<List<AreaListItem>>> GetRecAreaList()
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("https://api.live.bilibili.com/room/v1/Area/getRecList", "&actionKey=appkey", false);
                var results = await ApiClient.GetResults(apiInfo);
                var model = results.ToDynamicJObject();
                if (model.IsNotEmpty())
                {
                    if (model.Code == 0)
                    {
                        var m = model.Json["data"].ToObject<List<AreaListItem>>();
                        return BuildSuccessResult(m);
                    }
                    else
                        return BuildFailedResult<List<AreaListItem>>(model.Message);
                }
                else
                    return BuildFailedResult<List<AreaListItem>>();
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("GetRecAreaList", ex);
                return BuildExceptionResult<List<AreaListItem>>(ex);
            }
        }
        public async Task<ReturnModel<ReturnJObject>> GetNewData(int newIndex)
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("https://api.live.bilibili.com/room/v3/Area/getRoomList", $"&actionKey=appkey&area_id=0&cate_id=0&page={newIndex}&page_size=30&parent_area_id=0&qn=0&sort_type=live_time&tag_version=1", true);
                string results = await ApiClient.GetResults(apiInfo);
                var m = results.ToDynamicJObject();
                return BuildSuccessResult(m);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<ReturnJObject>(ex);
            }
        }
    }
}